package com.qpx.jxcmessage.confirm;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;


@Configuration
@Slf4j
public class CustomConfirmAndReturnCallback implements RabbitTemplate.ConfirmCallback {

    @Autowired
    RabbitTemplate rabbitTemplate;

    @PostConstruct
    public void init(){
        rabbitTemplate.setConfirmCallback(this);
    }

    /**
     * 根据 isSendSuccess判断 如果为true  则说明消息到达交换机
     * 如果为false  说明消息未到达交换机
     * @param correlationData  唯一的值
     * @param isSendSuccess   消息发送成功与否
     * @param error   出现异常的原因
     */
    @Override
    public void confirm(CorrelationData correlationData, boolean isSendSuccess, String error) {
        log.info("correlationData---------->:"+correlationData.getId());
        if (isSendSuccess){
            log.info("------------消息id:{},返送成功到达交换机............................................",correlationData.getId());
        }else {
            log.info("-------------消息id:{},未到达交换机,异常原因是{}...........................................",error);
        }
    }
}
